package kyle.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import kyle.test.CameraConnection.ResponseHandler;
import kyle.test.ServerConnection.ServerURL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

public class ServerConnect {

	private static final String APPLICATION_JSON = "application/json";
	private static final String CONTENT_TYPE_TEXT_JSON = "text/json";
	private final static String URL = "http://121.40.142.208:8080/AppServer/getCode.html";

	public static void main1(String[] args) {
		JSONObject object = new JSONObject();
		List params = new ArrayList();
		try {
			object.put("phone", "18501700216");
			NameValuePair pair1 = new BasicNameValuePair("data", object.toString());
			params.add(pair1);
//			HttpRequest.doPost(URL,params);
//			httpPostWithJSON(URL, object.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.print(ServerConnection.GetCode("18501700216")+"\n");
		System.out.print(ServerConnection.Register("18501700216", "123456", "702838", new File("test.png"))+"\n");
//		result userid = "03022011566220000";
//		System.out.print(ServerConnection.Login("18501700216", "123456")); 
//		result = "{"data":{"userid":"03022011566220000","userhead":"http://121.40.142.208:8080/AppServer/Image/20150302201156522/test.png","md5":"832993a45d565249f29ba9c2c6d32b50","userpattern":"101"},"msg":"成功","success":"true"}
//		System.out.print(ServerConnection.UpdatePassword(ciphertext, newpassword, password, phone, userid, code));
//		result failed
//		System.out.print(ServerConnection.UpdateNick("TestAccout", "03022011566220000"));
//		result = {"msg":"修改昵称成功","success":"true"}
//		System.out.print(ServerConnection.UpdateHead("03022011566220000", new File("test1.png")));
//		result = {"msg":"修改昵称成功","success":"true"}
		
	}
	
	
	public static void main3(String[] args){
		CameraConnection mCamera = new CameraConnection(new ResponseHandler() {
			
			@Override
			public void handleResponse(int request_code, String response) {
				// TODO Auto-generated method stub
				
				if(request_code == 105){
					String[] files = response.split("\n");
					System.out.print("files are "+ files.length+"\n");
				}
			}
		});
		
		mCamera.getFirmwareVersion(); //result = 1.0.0
//		mCamera.setTime("200109110911.33"); result = Tue Sep 11 09:11:33 UTC 2001OK, sync to hardware. 
//		mCamera.sendGPS(23.1244556f, 45.232134f); result = ""
//		mCamera.getFilelist(); result = 105 response is rtsp://192.168.1.1/media/19700101_000014.h264rtsp://192.168.1.1/media/19700101_000015.h264rtsp://192.168.1.1/media/19700101_000016.h264rtsp://192.168.1.1/media/19700101_000139.h264rtsp://192.168.1.1/media/19700101_000246.h264rtsp://192.168.1.1/media/19700101_000430.h264rtsp://192.168.1.1/media/19700101_002106.h264rtsp://192.168.1.1/media/19700101_002139.h264rtsp://192.168.1.1/media/20010911_091133.h264
//		mCamera.rebootDevice(); result =  OK, rebooting
		
		mCamera.setPassword("12345678");
	}
}
